package com.example.covid19.util.postexcel;

import cn.dev33.satoken.stp.StpUtil;
import com.example.covid19.common.Dto.Ctostu;
import com.example.covid19.common.lang.Result;
import com.example.covid19.mapper.AdministrateurMapper;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/excel")
public class ExcelTest2 {
    private final AdministrateurMapper administrateurMapper;

    public ExcelTest2(AdministrateurMapper administrateurMapper) {
        this.administrateurMapper = administrateurMapper;
    }

    /**
     * 导出未打卡的学生（当前管理员管理的）
     *
     * @param httpServletRequest
     * @throws IOException
     */
    @RequestMapping("/putlist")
    public void putlist(HttpServletRequest httpServletRequest) throws IOException {
        String s = (String) StpUtil.getLoginIdByToken(httpServletRequest.getHeader("satoken"));
        List<Ctostu> ctostuList = administrateurMapper.getcheck0(Integer.valueOf(s));
        System.out.println(ctostuList);
        XSSFWorkbook workbook = new XSSFWorkbook();
        //创建工作表，指定工作表名称
        LocalDate localDate = LocalDate.now();
        XSSFSheet sheet = workbook.createSheet(localDate + "未打卡学生表");
        XSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("班级");
        row.createCell(2).setCellValue("专业");
        for (int i = 0; i < ctostuList.size(); i++) {
            XSSFRow row1 = sheet.createRow(i + 1);
            row1.createCell(0).setCellValue(ctostuList.get(i).getName());
            row1.createCell(1).setCellValue(ctostuList.get(i).getClname());
            row1.createCell(2).setCellValue(ctostuList.get(i).getRname());
        }
        FileOutputStream out = new FileOutputStream("E:/mysql/" + localDate + "未打卡学生表.xls");
        workbook.write(out);
        out.flush();
        out.close();
        workbook.close();
    }

    @RequestMapping("sendfile")
    public void sendfile(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        LocalDate localDate = LocalDate.now();
        File file = new File("E:/mysql/" + localDate + "未打卡学生表.xls");
        try (InputStream inputStream = new FileInputStream(file);
             OutputStream outputStream = httpServletResponse.getOutputStream();) {
            httpServletResponse.setContentType("application/x-download");
            httpServletResponse.addHeader("Content-Disposition", "attachment;filename=" + localDate + "未打卡学生表.xls");
            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
